/*
 * EnigmaGM - A feature rich guild management system.
 * Copyright (C) 2007 ajiaojr.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
 * 02110-1301, USA.
 */
package org.ajiaojr.enigmagm.client;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author ajiaojr
 */
public abstract class Paged {
  /**
   * Index number of the first result.
   */
  protected int firstResult = 0;

  /**
   * Maximum number of results per page.
   */
  protected int maxResults;

  protected HorizontalPanel navBar;

  protected Panel panel;

  /**
   * @param panel
   * @param maxResults
   */
  public Paged(Panel panel, int maxResults) {
    this.panel = panel;
    this.maxResults = maxResults;
    createNavBar();
  }

  /**
   * Jumps to the first of all results.
   */
  public void head() {
    if (firstResult > 0) {
      firstResult = 0;
      populate();
    }
  }

  /**
   * Jumps to next page.
   */
  public void next() {
    firstResult += maxResults;
    populate();
  }

  /**
   * Populates the instance with the correct results.
   */
  public abstract void populate();

  /**
   * Jumps to previous page.
   */
  public void previous() {
    if (firstResult > 0) {
      if (firstResult > maxResults) {
        firstResult -= maxResults;
      } else {
        firstResult = 0;
      }
      populate();
    }
  }

  /**
   * Attaches a navigation bar.
   */
  protected void attachNavBar() {
    panel.add(navBar);
  }

  private void createNavBar() {
    navBar = new HorizontalPanel();
    navBar.setSpacing(5);

    final HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
    navBar.add(horizontalPanel_2);

    final Button raidsJumpToHeadButton = new Button();
    horizontalPanel_2.add(raidsJumpToHeadButton);
    raidsJumpToHeadButton.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        head();
      }
    });
    raidsJumpToHeadButton.setText("|<<");

    final Button raidsPreviousPageButton = new Button();
    horizontalPanel_2.add(raidsPreviousPageButton);
    raidsPreviousPageButton.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        previous();
      }
    });
    raidsPreviousPageButton.setText("<");

    final HorizontalPanel horizontalPanel_3 = new HorizontalPanel();
    navBar.add(horizontalPanel_3);
    navBar.setCellHorizontalAlignment(horizontalPanel_3,
        HasHorizontalAlignment.ALIGN_RIGHT);
    horizontalPanel_3
        .setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

    final Button raidsNextPageButton = new Button();
    horizontalPanel_3.add(raidsNextPageButton);
    raidsNextPageButton.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        next();
      }
    });
    raidsNextPageButton.setText(">");
  }
}
